#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Created: Tue May 18 19:08:55 2010

from sys import argv
from PyQt4 import QtCore, QtGui

class FrameServiceHandler(QtGui.QFrame):
    def __init__(self, connection, parent = None):
        super(FrameServiceHandler, self).__init__(parent)
        self.setupUi(self)
        
        self.connection = connection
        self.serviceHandler = self.connection.root.Modules.ServiceHandler.ServiceHandler()
        
    def setupUi(self, Service_handler_frame):
        Service_handler_frame.setObjectName("Service_handler_frame")
        Service_handler_frame.resize(819, 580)
        Service_handler_frame.setGeometry(QtCore.QRect(210, 45, 831, 581))
        Service_handler_frame.setFrameShape(QtGui.QFrame.StyledPanel)
        Service_handler_frame.setFrameShadow(QtGui.QFrame.Raised)
        self.gridLayout_3 = QtGui.QGridLayout(Service_handler_frame)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.btn_showAllServices = QtGui.QPushButton(Service_handler_frame)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("./icons/edit-find.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_showAllServices.setIcon(icon)
        self.btn_showAllServices.setObjectName("btn_showAllServices")
        self.gridLayout.addWidget(self.btn_showAllServices, 0, 0, 1, 1)
        spacerItem = QtGui.QSpacerItem(268, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 0, 1, 1, 1)
        self.listWidget_services = QtGui.QListWidget(Service_handler_frame)
        self.listWidget_services.setObjectName("listWidget_services")
        self.listWidget_services.setFixedSize(500, 580)
        self.gridLayout.addWidget(self.listWidget_services, 1, 0, 1, 2)
        self.gridLayout_3.addLayout(self.gridLayout, 0, 0, 2, 1)
        self.groupBox_3 = QtGui.QGroupBox(Service_handler_frame)
        self.groupBox_3.setObjectName("groupBox_3")
        self.groupBox_3.setFixedSize(430, 200)
        self.gridLayout_2 = QtGui.QGridLayout(self.groupBox_3)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.btn_startService = QtGui.QPushButton(self.groupBox_3)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("./icons/dialog-ok-apply.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_startService.setIcon(icon1)
        self.btn_startService.setObjectName("btn_startService")
        self.gridLayout_2.addWidget(self.btn_startService, 0, 0, 1, 1)
        self.btn_stopService = QtGui.QPushButton(self.groupBox_3)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("./icons/application-exit.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_stopService.setIcon(icon2)
        self.btn_stopService.setObjectName("btn_stopService")
        self.gridLayout_2.addWidget(self.btn_stopService, 1, 0, 1, 1)
        self.btn_restartService = QtGui.QPushButton(self.groupBox_3)
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap("./icons/dialog-ok.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_restartService.setIcon(icon3)
        self.btn_restartService.setObjectName("btn_restartService")
        self.gridLayout_2.addWidget(self.btn_restartService, 2, 0, 1, 1)
        self.btn_serviceStatus = QtGui.QPushButton(self.groupBox_3)
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap("./icons/document-preview.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_serviceStatus.setIcon(icon4)
        self.btn_serviceStatus.setObjectName("btn_serviceStatus")
        self.gridLayout_2.addWidget(self.btn_serviceStatus, 3, 0, 1, 1)
        self.gridLayout_3.addWidget(self.groupBox_3, 0, 1, 1, 1)
        spacerItem1 = QtGui.QSpacerItem(20, 361, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout_3.addItem(spacerItem1, 1, 1, 1, 1)

        ##########################   Signals Connections   ############################
        
        self.connect(self.btn_showAllServices, QtCore.SIGNAL('clicked()'), self.showAllServices)
        self.connect(self.btn_startService, QtCore.SIGNAL('clicked()'), self.startService)
        self.connect(self.btn_stopService, QtCore.SIGNAL('clicked()'), self.stopService)
        self.connect(self.btn_serviceStatus, QtCore.SIGNAL('clicked()'), self.serviceStatus)
        self.connect(self.btn_restartService, QtCore.SIGNAL('clicked()'), self.restartService)
        ########################################################################################

        self.retranslateUi(Service_handler_frame)
        QtCore.QMetaObject.connectSlotsByName(Service_handler_frame)

    def retranslateUi(self, Service_handler_frame):
        Service_handler_frame.setWindowTitle(QtGui.QApplication.translate("Service_handler_frame", "Frame", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_showAllServices.setText(QtGui.QApplication.translate("Service_handler_frame", "Show all services", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_3.setTitle(QtGui.QApplication.translate("Service_handler_frame", "Operations", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_startService.setText(QtGui.QApplication.translate("Service_handler_frame", "Start selected service", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_stopService.setText(QtGui.QApplication.translate("Service_handler_frame", "Stop selected service", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_restartService.setText(QtGui.QApplication.translate("Service_handler_frame", "Restart selected service", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_serviceStatus.setText(QtGui.QApplication.translate("Service_handler_frame", "Get selected service status", None, QtGui.QApplication.UnicodeUTF8))



    def showAllServices(self):
        '''
        showAllServices(self)
            show all services on the system
        '''
        self.listWidget_services.clear()
        servicesList = self.serviceHandler.getServices()
        for service in servicesList:
            if service == "README":
                continue
            else:
                self.listWidget_services.addItem(service)



    def startService(self):
        '''
        startService(self)
            start selected service
        '''
        currentService = str(self.listWidget_services.currentItem().text())
        if self.serviceHandler.start(currentService):
            QtGui.QMessageBox.information(self, "Information","service "+currentService+" is started")
        else:
            QtGui.QMessageBox.critical(self, "Exception","can't start the selected service")


        
    def stopService(self):
        '''
        stopService(self)
            stop selected service
        '''
        currentService = str(self.listWidget_services.currentItem().text())
        if self.serviceHandler.stop(currentService):
            QtGui.QMessageBox.information(self, "Information","service "+currentService+" is stopped")
        else:
            QtGui.QMessageBox.critical(self, "Exception","can't stop the selected service")



    def restartService(self):
        '''
        restartService(self)
            restart selected service
        '''
        currentService = str(self.listWidget_services.currentItem().text())
        if self.serviceHandler.restart(currentService):
            QtGui.QMessageBox.information(self, "Information","service "+currentService+" is restarted")
        else:
            QtGui.QMessageBox.critical(self, "Exception","can't restart the selected service")



    def serviceStatus(self):
        '''
        statusService(self)
            get status of the selected service
        '''
        currentService = str(self.listWidget_services.currentItem().text())
        serviceStatus = str(self.serviceHandler.status(currentService))
        if serviceStatus:
            QtGui.QMessageBox.information(self, "Information",serviceStatus)
        else:
            QtGui.QMessageBox.critical(self, "Exception","can't get the selected service status")

    
    
if __name__ == '__main__':
    app = QtGui.QApplication(argv)
    abstract_window = FrameServiceHandler()
    abstract_window.show()
    app.exec_()
